﻿Option Strict On
Option Explicit On
Option Infer On

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml.Linq
Imports GenDotNet.TemplateSupport
Imports GenDotNet.Task.Common
Imports GenDotNet.TemplateSupport.StandardAttributeExtensions
Imports GenDotNet.TemplateSupport.VB
Imports <xmlns="http://kadgen.com/tempCode.xsd">


Public Class Support
   Private Const vbcrlf As String = Microsoft.VisualBasic.ControlChars.CrLf

   'Public Shared Function StandardVBClassOpening( _
   '         ByVal className As String) _
   '         As String
   '   Return StandardVBClassOpening(className, Nothing, Nothing, Nothing)
   'End Function

   'Public Shared Function StandardVBClassOpening( _
   '         ByVal className As String, _
   '         ByVal importList As List(Of String)) _
   '         As String
   '   Return StandardVBClassOpening(className, importList, Nothing, Nothing)
   'End Function

   'Public Shared Function StandardVBClassOpening( _
   '         ByVal className As String, _
   '         ByVal importList As List(Of String), _
   '         ByVal attributes As StandardAttributes) _
   '         As String
   '   Return StandardVBClassOpening(className, importList, attributes, Nothing)
   'End Function

   'Public Shared Function StandardVBClassOpening( _
   '         ByVal className As String, _
   '         ByVal importList As List(Of String), _
   '         ByVal attributes As StandardAttributes, _
   '         ByVal nspace As String) _
   '         As String
   '   Dim code = <code>
   '                 <%= StandardVBOpening(importList, nspace) %>
   '                 <%= attributes.OutputVb() %>
   '                 <%= "" %>   Public Class <%= className %>
   '              </code>
   '   Return code.Value
   'End Function

   'Public Shared Function StandardVBInterfaceOpening( _
   '         ByVal interfaceName As String) _
   '         As String
   '   Return StandardVBInterfaceOpening(interfaceName, Nothing, Nothing, Nothing)
   'End Function

   'Public Shared Function StandardVBInterfaceOpening( _
   '         ByVal interfaceName As String, _
   '         ByVal importList As List(Of String), _
   '         ByVal attributes As StandardAttributes) _
   '         As String
   '   Return StandardVBInterfaceOpening(interfaceName, importList, attributes, Nothing)
   'End Function

   'Public Shared Function StandardVBInterfaceOpening( _
   '         ByVal interfaceName As String, _
   '         ByVal importList As List(Of String), _
   '         ByVal attributes As StandardAttributes, _
   '         ByVal nspace As String) _
   '         As String
   '   Dim code = <code>
   '                 <%= StandardVBOpening(importList, nspace) %>
   '                 <%= attributes.OutputVb() %>
   '                 <%= "" %>   Public Interface <%= interfaceName %>
   '              </code>
   '   Return code.Value
   'End Function

   'Public Shared Function StandardVBOpening( _
   '          ByVal importList As List(Of String)) _
   '          As String
   '   Return StandardVBOpening(importList, Nothing)

   'End Function

   '   Public Shared Function StandardVBOpening( _
   '            ByVal importList As List(Of String), _
   '            ByVal nspace As String) _
   '            As String
   '      Dim code = <code>
   'Option Strict On
   'Option Explicit On
   'Option Infer On

   'Imports System
   '<%= OutputImports(importList) %>
   '                    <%= OutputNamespace(nspace) %>
   '                 </code>
   '      Return code.Value
   '   End Function

   '   Public Shared Function StandardVBClassClose() As String
   '      Return StandardVBClassClose(Nothing)
   '   End Function

   '   Public Shared Function StandardVBClassClose(ByVal nspace As String) As String
   '      Return <code>
   '   End Class
   '<%= If(String.IsNullOrEmpty(nspace), "", <code> 
   'End Namespace
   '                                         </code>.Value) %>
   '             </code>.Value
   '   End Function

   '   Public Shared Function StandardVBInterfaceClose() As String
   '      Return StandardVBInterfaceClose(Nothing)
   '   End Function

   '   Public Shared Function StandardVBInterfaceClose(ByVal nspace As String) As String
   '      Return <code>
   '   End Interface
   '<%= If(String.IsNullOrEmpty(nspace), "", <code> 
   'End Namespace
   '                                         </code>.Value) %>
   '             </code>.Value
   '   End Function

   'Public Shared Function GetSerializeAttributeString() As String
   '   Return "<Serializable> _"
   'End Function

   '   Public Shared Function OutputNamespace(ByVal nspace As String) As String
   '      Return <code>
   '                <%= If(String.IsNullOrEmpty(nspace), "", _
   '                   <code>
   'Namespace <%= nspace %>
   '                   </code>.Value) %>
   '             </code>.Value
   '   End Function

   '   Public Shared Function OutputImports(ByVal importList As List(Of String)) As String
   '      Return <code>
   '                <%= From import In importList Select _
   '                   <code>
   'Imports <%= import %></code> %>.Value
   '</code>.Value & vbCrLf
   '   End Function

   Public Shared Function OutputFunctionOpen( _
            ByVal scope As Scope, _
            ByVal functionName As String, _
            ByVal returnType As String, _
            ByVal parameters As List(Of ParamInfo)) _
            As String
      Return <code>
                <%= New String(" "c, 9) %><%= scope.Output() %> Function <%= functionName %><%= OutputParameterDeclarations(parameters) %> _
                  As <%= returnType %>
             </code>.Value
   End Function

   Public Shared Function OutputSubOpen( _
            ByVal scope As Scope, _
            ByVal subName As String, _
            ByVal parameters As List(Of ParamInfo)) _
            As String
      Return <code>
                <%= New String(" "c, 9) %><%= scope.Output() %> Sub <%= subName %><%= OutputParameterDeclarations(parameters) %>
             </code>.Value
   End Function

   Public Shared Function OutputParameterDeclarations(ByVal parameters As List(Of ParamInfo)) As String
      If parameters.Count = 0 Then
         Return "()"
      Else
         Return <code>( _
<%= From par In parameters _
   Let last = parameters.Last _
   Select _
   <code>                  ByVal <%= par.name %> As <%= par.type %><%= CommaListEnding(last, par) %></code>.Value %>)</code>.Value
      End If
   End Function

   Public Shared Function OutputParameterList(ByVal parameters As List(Of ParamInfo)) As String
      If parameters.Count = 0 Then
         Return "()"
      Else
         Return <code>(<%= From par In parameters _
                          Let last = parameters.Last _
                          Select _
                          <code><%= " " %><%= par.name %><%= CommaListEndingNoWrap(last, par) %>
                          </code>.Value %>)</code>.Value
      End If
   End Function

   Public Shared Function CommaListEndingNoWrap(ByVal last As Object, ByVal par As Object) As String
      Return CommaListEnding(last, par, False)
   End Function

   Public Shared Function CommaListEnding(ByVal last As Object, ByVal par As Object) As String
      Return CommaListEnding(last, par, True)
   End Function

   Public Shared Function CommaListEnding(ByVal last As Object, ByVal par As Object, ByVal wrap As Boolean) As String
      If last IsNot Nothing AndAlso last IsNot par Then
         Return ", " & If(wrap, "_" & vbcrlf, "")
      Else
         Return String.Empty
      End If
   End Function

   Public Shared Function LiteralEndingNoWrap( _
             ByVal literal As String, _
             ByVal last As Object, _
             ByVal par As Object) _
             As String
      Return LiteralEnding(literal, last, par, False)
   End Function

   Public Shared Function LiteralEnding( _
            ByVal literal As String, _
            ByVal last As Object, _
            ByVal par As Object) _
            As String
      Return LiteralEnding(literal, last, par, True)
   End Function

   Public Shared Function LiteralEnding( _
            ByVal literal As String, _
            ByVal last As Object, _
            ByVal par As Object, _
            ByVal wrap As Boolean) _
            As String
      If last IsNot Nothing AndAlso last IsNot par Then
         Return literal & If(wrap, " _" & vbcrlf, "")
      Else
         Return String.Empty
      End If
   End Function

   Public Shared Function GetEnumValue(Of T)(ByVal value As String) As T
      If String.IsNullOrEmpty(value) Then
         Return CType([Enum].Parse(GetType(T), "None"), T)
      End If
      Return CType([Enum].Parse(GetType(T), value), T)
   End Function
End Class
